package com.dongke.service.Impl;

import com.dongke.mapper.Role1Mapper;
import com.dongke.mapper.User1Mapper;
import com.dongke.mapper.User1Role1Mapper;
import com.dongke.pojo.Role1;
import com.dongke.pojo.User1;
import com.dongke.pojo.User1Example;
import com.dongke.pojo.User1Role1Example;
import com.dongke.service.User_1Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class User_1ServiceImpl implements User_1Service {
    @Autowired
    private User1Mapper user1Mapper;
    @Autowired
    private User1Role1Mapper user1Role1Mapper;
    @Autowired
    private Role1Mapper role1Mapper;
    @Override
    public PageInfo<User1> selectInfo(User1 user, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<User1> list = user1Mapper.selectByName(user);
        PageInfo<User1> userPageInfo = new PageInfo<>(list);
        return userPageInfo;
    }

    @Override
    public User1 getUserInfo(User1 user) {
        User1Example userExample = new User1Example();
        User1Example.Criteria criteria = userExample.createCriteria();
        criteria.andUnameEqualTo(user.getUname());
        List<User1> user1s = user1Mapper.selectByExample(userExample);
        if(user1s != null && user1s.size()!= 0){
            return user1s.get(0);
        }
        return null;
    }

    @Override
    public boolean addUserInfo(User1 user, Integer[] rids) {
       int i = user1Mapper.inserInfoReturnId(user);
        System.out.println(i);
        int i1 = user1Role1Mapper.insertBatch(user.getUid(), rids);
        return i1 > 0;
    }

    @Override
    public User1 getInfo(Integer uid) {
        User1 user1 = user1Mapper.selectByPrimaryKey(uid);
        return user1;
    }

    @Override
    public List<Role1> getRoleInfo() {
        return role1Mapper.selectByExample(null);
    }

    @Override
    public List<Integer> getUserRoleById(Integer uid) {

        return user1Role1Mapper.getUserRoleById(uid);
    }

    @Override
    public Boolean updateUserById(User1 user, Integer[] rid) {
        user1Mapper.updateByPrimaryKeySelective(user);
        User1Role1Example user1Role1Example = new User1Role1Example();
        User1Role1Example.Criteria criteria = user1Role1Example.createCriteria();
        criteria.andUidEqualTo(user.getUid());
        user1Role1Mapper.deleteByExample(user1Role1Example);
        int i = user1Role1Mapper.insertBatch(user.getUid(), rid);
        return i>0;
    }

    @Override
    public boolean deleteInfoById(Integer uid) {
        user1Mapper.deleteByPrimaryKey(uid);
        user1Role1Mapper.deleteByUid(uid);

        return true;
    }
}
